import java.util.HashMap;
import java.util.Map;

public class test4 {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer,Integer> hash = new HashMap<>();
        for(int i = 0;i<nums.length;i++){
            if(hash.containsKey(nums[i])){
                int j = hash.get(nums[i]);
                if(Math.abs(i-j)<=k){
                    return true;
                }
            }else{
                hash.put(nums[i],i);
            }

        }
        return false;
    }
}
